//
//  CYYSetViewController.m
//  LetsParty
//
//  Created by rimi on 15/9/14.
//  Copyright (c) 2015年 LetsPartyTeam. All rights reserved.
//

#import "CYYSetViewController.h"
#import "CYYTableViewCell.h"
#import "CYYHistoryViewController.h"
#import "CYYAboutUsViewController.h"
#import "CYYChangeInfoViewController.h"
#import "CYYLoadViewController.h"

enum{
    editFirstButtonTag = 100,
    editSecondButtonTag,
};

static NSString * kCAIdentiferCell = @"cellIdf";
//name font size
static NSInteger nameFontSize = 22;
//phoneNumber font size
static NSInteger phoneNumberFontSize = 15;

@interface CYYSetViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView * mainTableView;
@property (nonatomic, strong) UIButton * photoButton;//头像
@property (nonatomic, strong) UILabel * nameLabel;//名字
@property (nonatomic, strong) UILabel * phoneNumberLabel;//手机号
@property (nonatomic, strong) UIButton * editButton;//编辑资料Button
//@property (nonatomic, strong) UIVisualEffectView * cVisualEffectView;//模糊背景

@property (nonatomic, strong) UIView * bacView;
- (void)initializeUserInterface;//个人中心视图

@end

@implementation CYYSetViewController
#pragma mark - life cycle       生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
//    self.view.backgroundColor = [UIColor whiteColor];
//    self.baseNavigationBar.backgroundColor = [UIColor whiteColor];
//    self.bartitleLabel.text = @"设置";
    [self initializeUserInterface];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    [self.mainTableView reloadData];
    [self updateUserInfoDate];
    self.mainTableView.contentOffset = DHFlexibleCenter(CGPointMake(0, 0));
}

#pragma mark - system protocol implentations
//行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSNumber * cNumber = @[@4,@2][section];
    return [cNumber integerValue];
}
//组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 20.f;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 270 * DHFlexibleVerticalMutiplier();
    }else{
        return 1.f;
    }
}

//head view
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 0) {
       return [self userHeadView];
    }else{
        return nil;
    }
}

//cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:kCAIdentiferCell];
    cell.selectionStyle = UITableViewCellEditingStyleNone;
    cell.textLabel.text = @[@[@"所有历史活动",@"发起的历史活动",@"参加的历史活动",@"关注的历史活动"],@[@"关于LetsParty",@"                    退出当前账号"]][indexPath.section][indexPath.row];
    cell.backgroundColor = [UIColor whiteColor];
    NSArray * imageNameArray = @[@[@"allp.png",@"kuw",@"cup",@"kzf"],@[@"ltb",@"no=="]];
    cell.imageView.image = [UIImage imageNamed:imageNameArray[indexPath.section][indexPath.row]];
    //配置accessoryView
    cell.accessoryView = [self cAccaccessoryViewWithImageName:@"iconfont-jiantouyou"];
    if (indexPath == [NSIndexPath indexPathForRow:1 inSection:1]) {
        cell.textLabel.textColor = [UIColor redColor];
        cell.accessoryView = nil;
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50.f * DHFlexibleVerticalMutiplier();
}

//点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    CYYHistoryViewController * detailVC = [[CYYHistoryViewController alloc]init];
    CYYAboutUsViewController * aboutUsVC = [[CYYAboutUsViewController alloc]init];
    switch (indexPath.section) {
        case 0:{
            detailVC.index = indexPath.row;
            [[ControllerManager sharedManager].rootViewController pushViewController:detailVC animated:YES];
        }
            break;
        case 1:{
            if (indexPath.row == 0) {
                [[ControllerManager sharedManager].rootViewController pushViewController:aboutUsVC animated:YES];
            }else{
                //注销
                [CYYNetWorkManager signOut];
                [self alterViewWithMessage:@"退出登录"];
                [[[ControllerManager sharedManager] mainTabController] goToViewWithIndex:0];
            }
        }
            break;
    }
}

#pragma mark - private methods
- (void)initializeUserInterface{
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self.view addSubview:self.mainTableView];
}

- (void)alterViewWithMessage:(NSString *)message{
    UIAlertController * alterViewController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:message preferredStyle:UIAlertControllerStyleAlert];
    [alterViewController addAction:[UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        [alterViewController dismissViewControllerAnimated:YES completion:nil];
    }]];
    [self presentViewController:alterViewController animated:YES
                     completion:nil];
}

- (UIView *)cAccaccessoryViewWithImageName:(NSString *)imageNameString{
    UIImageView * cImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:imageNameString]];
    cImageView.frame = DHFlexibleFrame(CGRectMake(0, 0, 20, 20), NO);
    return cImageView;
}

//head view
- (UIView *)userHeadView{
    UIImageView * cImageView = [[UIImageView alloc]init];
    cImageView.frame = DHFlexibleFrame(CGRectMake(0, 0, 320, 270), NO);
    cImageView.image = [UIImage imageNamed:@"loadview-1"];
    cImageView.userInteractionEnabled = YES;
    cImageView.tag = 1000;
    [cImageView addSubview:self.bacView];
//    [cImageView addSubview:self.cVisualEffectView];
    [cImageView addSubview:self.photoButton];
    [cImageView addSubview:self.nameLabel];
    [cImageView addSubview:self.phoneNumberLabel];
    [cImageView addSubview:self.editButton];
    [self.editButton addTarget:self action:@selector(respondsToEditButton:) forControlEvents:UIControlEventTouchUpInside];
    return cImageView;
}

//label
- (UILabel *)haveLabelByFrame:(CGRect)frame{
    UILabel * cLabel = [[UILabel alloc]initWithFrame:frame adjustWidth:NO];
    cLabel.textAlignment = NSTextAlignmentCenter;
    cLabel.textColor = [UIColor whiteColor];
    return cLabel;
}

//update userinfo date
- (void)updateUserInfoDate{
    self.nameLabel.text = [[CYYNetWorkManager currentUserGet] objectForKey:@"name"];
    self.phoneNumberLabel.text = [NSString stringWithFormat:@"手机号:%@",[[CYYNetWorkManager currentUserGet] objectForKey:@"mobilePhoneNumber"]];
    [self.photoButton setImage:[UIImage imageNamed:@"loadview-1"] forState:UIControlStateNormal];
}

#pragma mark - responds event
- (void)respondsToEditButton:(UIButton *)button{
    CYYChangeInfoViewController * changeVC =[[CYYChangeInfoViewController alloc]init];
    [[ControllerManager sharedManager].rootViewController pushViewController:changeVC animated:YES];
}

#pragma mark - getter
- (UITableView *)mainTableView{
    if (!_mainTableView) {
        _mainTableView = ({
            UITableView * tableView = [[UITableView alloc]initWithFrame:DHFlexibleFrame(CGRectMake(0, 0, 320, 524), NO) style:UITableViewStyleGrouped];
            tableView.delegate = self;
            tableView.dataSource = self;
            [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cellIdf"];
            tableView.showsVerticalScrollIndicator = NO;
            tableView;
        });
    }
    return _mainTableView;
}

- (UIButton *)photoButton{
    if (!_photoButton) {
        _photoButton = ({
            UIButton * cButton = [UIButton buttonWithType: UIButtonTypeCustom];
            cButton.frame = DHFlexibleFrame(CGRectMake(110, 70, 100, 100), NO);
            cButton.layer.masksToBounds = YES;
            cButton.layer.cornerRadius = 50  * DHFlexibleVerticalMutiplier();
            cButton.userInteractionEnabled = NO;
            cButton;
        });
    }
    return _photoButton;
}

- (UILabel *)nameLabel{
    if (!_nameLabel) {
        _nameLabel = ({
            [self haveLabelByFrame:CGRectMake(60, 170, 200, 30)];
        });
        _nameLabel.font = [UIFont systemFontOfSize:nameFontSize];
    }
    return _nameLabel;
}

- (UILabel *)phoneNumberLabel{
    if (!_phoneNumberLabel) {
        _phoneNumberLabel = ({
            [self haveLabelByFrame:CGRectMake(60, 200, 200, 30)];
        });
        _phoneNumberLabel.font = [UIFont systemFontOfSize:phoneNumberFontSize];
    }
    return _phoneNumberLabel;
}

- (UIButton *)editButton{
    if (!_editButton) {
        _editButton = ({
            UIButton * cButton = [UIButton buttonWithType:UIButtonTypeCustom];
            cButton.frame = DHFlexibleFrame(CGRectMake(250, 30, 60, 30), NO);
            cButton.layer.cornerRadius = 10;
            cButton.layer.borderColor = [UIColor whiteColor].CGColor;
            cButton.layer.borderWidth = 0.5f;
            cButton.backgroundColor = [UIColor clearColor];
            [cButton setTitle:@"资料" forState:UIControlStateNormal];
            cButton;
        });
    }
    return _editButton;
}

//- (UIVisualEffectView *)cVisualEffectView{
//    if (!_cVisualEffectView) {
//        _cVisualEffectView = ({
//            UIBlurEffect * cBlurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
//            UIVisualEffectView * cVisualEffectView = [[UIVisualEffectView alloc]initWithEffect:cBlurEffect];
//            cVisualEffectView.frame = DHFlexibleFrame(CGRectMake(0, 0, 320, 270), NO);
//            cVisualEffectView.backgroundColor = COLOR_RGB(80, 134, 234, 0.2);
////            cVisualEffectView.alpha = 1.0;
////            cVisualEffectView.backgroundColor = [UIColor colorWithWhite:0.1 alpha:0.1];
//            cVisualEffectView;
//        });
//    }
//    return _cVisualEffectView;
//}

-(UIView *)bacView{
    if (!_bacView) {
        _bacView = ({
            UIView * cView = [[UIView alloc]initWithFrame:CGRectMake(0, -200, 320, 470) adjustWidth:NO];
            cView.backgroundColor = COLOR_RGB(80, 134, 234, 1);
            cView.alpha = 0.7;
            cView;
        });
    }
    return _bacView;
}

@end

